
/**
 * 下拉列表选择按钮
 */

Yacon.widget.ListContainer= function(conText)
{
	
	/**是否需要垫板*/
	this.isshim  = true;
	/**高度反向展现*/
	this.revTop  = false;
	/**宽度反向展现*/
	this.revLeft = false;
	
	this.slideRat = 50;
	
	this.ischeck  = true;
	
	this.selectList = new Array();
	
	/**参照物高度*/
	this.renderHeight = 0;	
	
	Yacon.apply(this,conText);
	
	this.create();
};

Yacon.widget.ListContainer.Extend(Yacon.widget.WinControl,{
		
	create:function()
	{
		
		this.element = document.createElement("div");		
		this.element.Control = this;		
		this.element.className="win_container";
		
		document.body.appendChild(this.element);
		
		this.addEvents("mouseleave",Yacon.widget.ListContainer.events.onMouseLeave);
	},   
   	
   onrender:function()
   {
		var prtop = 0;
		
		this.revTop  = false;
		this.revLeft = false;
		
		var abtheight = this.top + this.height;	        
		if (abtheight >document.body.clientHeight + document.body.scrollTop )
	    {		
			if (this.top > this.height)
			{
				this.revTop = true;				
			}else
				window.dialogHeight = (abtheight + 5) + "px";
	    }
			
		var abtwidth = this.left + this.width;
		if (abtwidth > document.body.clientWidth + document.body.scrollLeft)
		{		
			this.left    = document.body.clientWidth + document.body.scrollLeft-5;	
			this.revLeft = true;
		}
		//alert(this.revLeft)
		this.setStyle("height",0);
	    this.setStyle("width",this.width);
	    if (this.revLeft)
	    	this.setStyle("left",this.left-this.width);
	    else
	    	this.setStyle("left",this.left);
	    
	    prtop = this.top;
	    
	    if (!this.revTop)
	    	prtop = this.top -1;
	   
	  	    
    	this.setStyle("top",prtop);
    	
	    this.setStyle("zIndex",this.zindex);

	    if (this.isshim && !this.shim)
	    {
	    	this.shim = new Yacon.widget.Shim();
	    }
	    this.shim.setConText({width:this.width,height:this.height,left:this.left,top:prtop,revTop:this.revTop,revLeft:this.revLeft,zindex:this.zindex-2});

   },
   /**
	 * 添加窗体地址
	 */
   appenChildURl:function(url)
   {
		this.getfTemplate().src= this.getPath(url); 
		this.appenChild(this.getfTemplate());		
   },
 /**
  * 添加列表数组
  */
   appenChildList:function(options)
   {
	    var listTable = this.appenChild(this.getLTemplate(options));
		if (listTable)
			listTable.Control = this;	
   },   
   appenChild:function(element)
   {
	   this.innerElement = element;  
	   
	   return this.loadChild(element);
   },
   
   loadChild:function(element)
   {
	   this.onrender();
	   
	   if (Yacon.isObject(element))
		   this.element.appendChild(element);
	   else if (Yacon.isString(element))
		   this.element.innerHTML = element; 
	   
	   return this.element.children[0];
   },

   falshHide:function()
   {
	   this.setStyle("overflow","hidden");
	   this.setStyle("display","none");	     
	   if ( this.shim) 
		   this.shim.hide(0);
	   
	   this.isshow = false;
   },
   
   falshShow:function()
   {
	   
	   this.setStyle("height",this.height+"px");
	   if(this.revTop)
		   this.setStyle("top",(this.top - this.height)+"px");	
	   
	   if ( this.shim) 
		   this.shim.show(this.width,this.height);
	   
	   this.setStyle("overflow","auto");
	   this.setStyle("display","block");
	   this.isshow = true;
   },
   
   slideShow:function()
   {
	   
	   var parent = this;	  
	   var h = 0;
	   var w = 0;
	   var ofH = this.height
	   function slideS()
	   {
		   if (parent.element.children[0])
		   {
			   if (parent.element.children[0].offsetHeight >0)
			   {
				   ofH = parent.element.children[0].offsetHeight<parent.height?parent.element.children[0].offsetHeight:parent.height;
				   ofH +=5;
			   }
		  }
		   
		   h += parent.height/parent.slideRat;
		   w += parent.width/parent.slideRat;
		   if (h <= ofH)
		   {
			   parent.element.style.height = h + "px";
			   if(parent.revTop)
				   parent.element.style.top = (parent.top-h)+"px";		   
			   
			   parent.element.style.width= w + "px";
			   if(parent.revLeft)
				   parent.element.style.left  = (parent.left-w)+"px";			
			   
			   setTimeout(slideS, 5); 
		   }else
		   {
			   h = ofH;
			   w = parent.width;
			   
			   parent.element.style.height = h+"px";
			   if(parent.revTop)
				  parent.element.style.top = (parent.top - h)+"px";
			   
			   parent.element.style.width = w+"px";
			   if(parent.revLeft)
				  parent.element.style.left = (parent.left - w)+"px";
			   
			   parent.element.style.overflow = "auto";   
		   }		   
		   parent.element.style.display = "block";
		   
		   if (parent.shim)
			   parent.shim.show(w,h);
	   }	   
	   slideS();
	  
	   this.isshow = true;
   },  
 
   slideHide:function()
   {
	   this.setStyle("overflow","hidden");
	   
	   var parent = this;
	    
	   var ofH = parent.element.offsetHeight<this.height?parent.element.offsetHeight:this.height;
	   var ofW = this.width;
	  
	   var h  = ofH;
	   var w  = ofW;
	   function slideH()
	   {
		   h -= ofH/parent.slideRat;
		   w -= ofW/parent.slideRat;
		 
		   if (h <= 0 || w<=0)
		   { 
			   parent.element.style.display = "none";			  	   
		   }else 
		   {
		    	parent.element.style.height = h +"px";	
				if(parent.revTop)
					parent.element.style.top =(parent.top+h)+"px";
				
				parent.element.style.width = w + "px";	
				if(parent.revLeft)
					parent.element.style.left = (parent.left+w)+"px";
				
				setTimeout(slideH, 5); 
		   }
		   if (parent.shim) 
			   parent.shim.hide(w,h);		
	   }
	   slideH();
	  
	   this.isshow = false;
   },

  
  getfTemplate:function()
  {
	  if (!this.frameElement)
	  {
		  this.frameElement = document.createElement("iframe");
		  this.frameElement.Control = this;
		  this.frameElement.setAttribute("frameBorder","0");
	  }
	  this.frameElement.setAttribute("height",this.height);
	  return this.frameElement;
   },
   
   getLTemplate:function(options)
   {
   		listElement = '<table class="drop_list_table" cellpadding=1 cellspacing=1>';
	 	
   		var lists = new Array(options.length);
   	
   		var className ="";
   		for(var i=0;i<options.length;i++)
	 	{
   			if (i%2==0)
   				className = "tableOddRow";
   			else
   				className = "tableEvenRow";
   		
   			if (!this.ischeck)
   			{
   				var activeClass = className;
   				if (this.selectList.indexOf(options[i].key)>-1)
   					activeClass = "rowSelected";
   				
   				var text = options[i].value;
   				
   				lists[i] = '<tr><td class="'+activeClass+'" value="'+options[i].key+'"  text="'+options[i].value+'"   onClick="Yacon.widget.ListContainer.events.onListClick()" ' +
	 					  'onMouseOver="this.className=\'rowSelected\';" onMouseOut="this.className=\''+className+'\'" >&nbsp;'+
	 				      options[i].value+'&nbsp;</td></tr>';			
   			}else
   			{
   				var activeClass = "";
   				if (this.selectList.indexOf(options[i].key)>-1)
   					activeClass = "checked";
   			
   				lists[i] = '<tr><td class="'+className+'" value="'+options[i].key+'" text="'+options[i].value+'"   onClick="Yacon.widget.ListContainer.events.onListClick()" ' +
				  		'onMouseOver="this.className=\'rowSelected\';" onMouseOut="this.className=\''+className+'\'" >'+
				  		'<input type="checkbox" '+ activeClass +'>&nbsp;'+
				  		options[i].value+'&nbsp;</td></tr>';
   			}	    		
	 	}	 	
   	
      	listElement = listElement + lists.join("") + '</table>' 
      	
	 	return listElement;
   },
   
   listBroadcast:function(table,element)
   {
   		this.selectList.clear();			
		if (this.ischeck)
		{
			for(var i=0; i<table.rows.length;i++)
			{
				if (table.rows[i].cells[0].children[0] && table.rows[i].cells[0].children[0].checked)
				{
					this.selectList[this.selectList.length]= table.rows[i].cells[0].getAttribute("value");					
				}
			}
		}else
		{
			this.selectList[this.selectList.length]= element.getAttribute("value")
		}
		if(this.getAttachControl())
			this.getAttachControl().setFieldValue();
		
		
   },
   hidden:function()
   {
	   if(this.getAttachControl())
			this.getAttachControl().hidden();
   },
   destroy:function()
   {
	   this.element.clearAttributes();
	   this.remove(this);
	   delete this;	  	  
   }
    
});

Yacon.widget.ListContainer.events= {	
	onListClick:function()
	{
		var element   = event.srcElement;
		if (element.children[0])
			element.children[0].checked  = !element.children[0].checked ;	
			
		var tab = Yacon.getTable(element);
		if (tab.Control)
		{
			tab.Control.listBroadcast(tab,element);
			tab.Control.hidden();
		}
				
	},
	onMouseLeave:function()
	{
		var element   = event.toElement;
		
		if (element&& element.Control != window.activeElement)
		{
			if (event.srcElement.Control)
				event.srcElement.Control.hidden();
		}			
	}
}

Yacon.widget.Shim  = function(conText)
{
	
	this.isshow     = false;
	/**高度反向展现*/
	this.revTop  = false;
	/**宽带反向展现*/
	this.revLeft = false;
	
	Yacon.apply(this,conText);
	
	this.create();
	
};

Yacon.widget.Shim.Extend(Yacon.widget.WinControl,{	
		 
	create:function()
	{
		this.element = document.createElement("iframe");
		this.element.Control = this;
		this.element.setAttribute("frameBorder","0");
		this.element.setAttribute("scrolling","no");
	
		this.element.className="win_shim";			
		document.body.appendChild(this.element);
    },   
    setConText:function(conText)
	{
		Yacon.apply(this,conText);
		this.onrender();
	},
	onrender:function()
	{
		this.setStyle("zIndex",this.zindex);
		this.setStyle("width",this.width);
		this.setStyle("height",this.height);
	    this.setStyle("posLeft",this.left);
	    this.setStyle("posTop",this.top);
	},
    show:function(width,height)
    {
		this.element.style.height = height+"px"; 
		if(this.revTop)
			this.element.style.top = (this.top-height)+"px";    
		
		this.element.style.width = width +"px";
		if(this.revLeft)
			this.element.style.left = (this.left-width)+"px";		
		
		
		this.setStyle("display","block");
	  
   },
   hide:function(width,height)
   {
	  if (height <= 0 ||width <= 0)
		  this.setStyle("display","none");
	   else
	   {
		   this.element.style.height = height +"px";
		   if(this.revTop)
	    	   this.element.style.top = (this.top+height)+"px";
		   
		   this.element.style.width = width +"px";
		   if(this.revLeft)
				this.element.style.left = (this.left+width)+"px";		
			
	   }	  
   },
   destroy:function()
   {
	   this.element.clearAttributes();
	   this.remove(this);
	   delete this;	  	  
   }
});

if (!window.Droplist && document.body) 
	window.Droplist = new Yacon.widget.ListContainer();



